<template>
    <div class="container">
        <div class="handle-box">
                <el-form ref="form" :model="form" label-width="80px" class="flex">
                    <el-form-item label="小区：">
                        <el-input v-model="name" placeholder="请输入名称" class="handle-input mr10" @input="mohu()"></el-input>
                    </el-form-item>
                     <el-form-item label="楼号：">
                        <el-input v-model="name" placeholder="请输入名称" class="handle-input mr10" @input="mohu()"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" >搜索</el-button>
                        <el-button type="primary"  icon="el-icon-plus" @click="addlist()">新增</el-button>
                    </el-form-item>
                    
                </el-form>
            </div>
            <el-table
                :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
            >
                <el-table-column type="index" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="垃圾点名称"></el-table-column>
                <el-table-column prop="street" label="街道"></el-table-column>
                <el-table-column prop="community" label="小区"></el-table-column>
                <el-table-column prop="building" label="楼号"></el-table-column>
                <el-table-column prop="Latitude" label="经纬度"></el-table-column>
                <el-table-column prop="number" label="垃圾桶数"></el-table-column>
                <el-table-column prop="ifsmart" label="是否有智能垃圾桶"></el-table-column>
                <!-- <el-table-column label="兑换物品">
                    <template slot-scope="scope">{{scope.row.money}}</template>
                </el-table-column>
                <el-table-column prop="address" label="兑换类型"></el-table-column> -->
                <!-- <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.state==='成功'?'success':(scope.row.state==='失败'?'danger':'')"
                        >{{scope.row.state}}</el-tag>
                    </template>
                </el-table-column> -->

                <!-- <el-table-column prop="date" label="兑换积分"></el-table-column> -->
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            @click="handleDelete(scope.$index, scope.row)" style="color:red"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, sizes, prev, pager, next, jumper"
                    :current-page="currentPage"  
                    :page-size="pageSize"
                    :total="tableData.length"
                    @size-change="handleSizeChange"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
            <!-- 编辑弹出框 -->
            <el-dialog v-dialogDrag title="编辑" :visible.sync="editVisible" width="30%">
                <el-form ref="forms" :model="forms" label-width="100px">
                    <el-form-item label="垃圾点名称：">
                        <el-input v-model="forms.name" :label-width="labelwidth"></el-input>
                    </el-form-item>
                    <el-form-item label="街道：">
                        <el-input v-model="forms.street" :label-width="labelwidth"></el-input>
                    </el-form-item>
                    <el-form-item label="小区：">
                        <el-input v-model="forms.community" :label-width="labelwidth"></el-input>
                    </el-form-item>
                    <el-form-item label="楼号：">
                        <el-input v-model="forms.building" :label-width="labelwidth"></el-input>
                    </el-form-item>
                    <el-form-item label="经纬度：">
                        <el-input v-model="forms.Latitude" :label-width="labelwidth"></el-input>
                    </el-form-item>
                    <el-form-item label="垃圾桶数：">
                        <el-input v-model="forms.number" :label-width="labelwidth"></el-input>
                    </el-form-item>
                    <el-form-item label="是否有智能垃圾桶：" >
                         <el-input v-model="forms.ifsmart" :label-width="labelwidth"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </el-dialog>
            <!-- 新增弹出框 -->
            <el-dialog v-dialogDrag title="新增" :visible.sync="addeditVisibles" width="30%">
                <el-form ref="form" :model="form" label-width="100px">
                    <el-form-item label="垃圾点名称：">
                        <el-input v-model="form.name" :label-width="labelwidth"></el-input>
                    </el-form-item>
                    <el-form-item label="街道：">
                        <el-input v-model="form.street" :label-width="labelwidth"></el-input>
                    </el-form-item>
                    <el-form-item label="小区：">
                        <el-input v-model="form.community" :label-width="labelwidth"></el-input>
                    </el-form-item>
                    <el-form-item label="楼号：">
                        <el-input v-model="form.building" :label-width="labelwidth"></el-input>
                    </el-form-item>
                    <el-form-item label="经纬度：">
                        <el-input v-model="form.Latitude" :label-width="labelwidth"></el-input>
                    </el-form-item>
                    <el-form-item label="垃圾桶数：">
                        <el-input v-model="form.number" :label-width="labelwidth"></el-input>
                    </el-form-item>
                    <el-form-item label="是否有智能垃圾桶：">
                         <el-input v-model="form.ifsmart" :label-width="labelwidth"></el-input>
                    </el-form-item>
                    
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="addeditVisibles = false">取 消</el-button>
                    <el-button type="primary" @click="succadd">确 定</el-button>
                </span>
            </el-dialog>
        </div>
</template>
<script>
import '../../assets/css/common.css'

export default {
    data(){
        return{
              currentPage:1, //初始页
                pagesize:10,    //    每页的数据
                userList: [],
                address:'',
                name:'',
                editVisibles:false,
                labelwidth:'150',
                addeditVisibles:false,
                form:{
                    id: '',
                    name:'',
                    street: '',
                    community: '',
                    building: '',
                    Latitude: '',
                    number: '',
                    ifsmart: ''
                }, 
                forms:{
                    id: '',
                    name:'',
                    street: '',
                    community: '',
                    building: '',
                    Latitude: '',
                    number: '',
                    ifsmart: ''
                },
            tableData: [{
                name:'张三',
                street: '华苑街道',
                community: '蒂花里小区',
                building: '1号楼',
                Latitude: '117.36  37.6458',
                number: '23',
                ifsmart: '是'
            },{
                name:'贾',
                street: '华苑街道',
                community: '蒂花里小区',
                building: '6号楼',
                Latitude: '117.36  37.6458',
                number: '6',
                ifsmart: '否'
            },{
                name:'以',
                street: '华苑街道',
                community: '蒂花里小区',
                building: '11号楼',
                Latitude: '117.36  37.6458',
                number: '13',
                ifsmart: '是'
            },{
                name:'冰',
                street: '华苑街道',
                community: '蒂花里小区',
                building: '1号楼',
                Latitude: '117.36  37.6458',
                number: '23',
                ifsmart: '是'
            },{
                name:'定',
                street: '华苑街道',
                community: '蒂花里小区',
                building: '7号楼',
                Latitude: '117.36  37.6458',
                number: '23',
                ifsmart: '是'
            },{
                name:'张三封',
                street: '华苑街道',
                community: '蒂花里小区',
                building: '1号楼',
                Latitude: '117.36  37.6458',
                number: '23',
                ifsmart: '是'
            },{
                name:'东方不败',
                street: '华苑街道',
                community: '蒂花里小区',
                building: '1号楼',
                Latitude: '117.36  37.6458',
                number: '23',
                ifsmart: '否'
            }],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            forms: {
                name: ''
            },
            idx: -1,
            id: -1
        }
    },
    created() {
        // this.getData();
    },
    methods: {
        addlist(){
            this.addeditVisibles = true;
            
        },
        succadd(){
            this.$message.success('新增一行')
            this.tableData.unshift(this.form)
            this.editVisibles = false;
        },
        mohu() {
            var newdate = [];
            // console.log(this.name == '')
            if(this.name == ''){
                this.tableData.forEach(item=>{
                    console.log(item) 
                    newdate.push(item)
                })
            }else{
                this.tableData.filter(item=>{
                   if(item.name.indexOf(this.name) > -1){
                       newdate.push(item)
                   }
                })
            }
            this.tableData = newdate;

            // var search = this
            // console.log(search)
            // var newdate = [];
            // if(search){
            //     if(search)
            //      this.tableData.filter(item=>{
            //         if(item.name.indexOf(search) != -1){
            //             newdate.push(item)
            //         }
            //     })
            // }else if (search == ''){
            //     console.log(this.tableData)
            // }
            // this.tableData = newdate
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.forms = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(currentPage) {
            this.currentPage = currentPage;
            // this.$set(this.query, 'pageIndex', val);
            // this.getData();
        },
        handleSizeChange(size){
            this.pagesize = size;
        },
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
    }
}
</script>
<style scoped>

</style>